package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.entity.AgeRange;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 会员SERVICE实现类
 */
@Service(interfaceClass = MemberService.class)
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Transactional
    @Override
    public Member addIfNotExist(Member member) {
        log.info("[会员-添加]data:{}",member);
        Member pojo = memberDao.selectPhoneNumber(member.getPhoneNumber());
        if(null != pojo) {
            //会员已添加，直接返回
            return pojo;
        }
        //调用DAO插入数据
        memberDao.insert(member);
        return memberDao.selectById(member.getId());
    }

    @Override
    public Member findById(Integer id) {

        return memberDao.selectById(id);
    }

    @Override
    public List<Integer> countByMonth(List<String> months) {
        List<Integer> resultLIst = new ArrayList<>();
        for (String month : months) {
            String endDate = month+".31";
            Long count = memberDao.countByRegTimeBefore(endDate);
            resultLIst.add(count.intValue());
        }
        return resultLIst;
    }

    @Override
    public List<Integer> countByAgeRangeList(List<AgeRange> ageRangeList) {
        List<Integer> resultList = new ArrayList<>();
        for (AgeRange ageRange : ageRangeList) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.YEAR, -ageRange.getMinAge());
            Date endDate = calendar.getTime();

            calendar.add(Calendar.YEAR, ageRange.getMinAge()-ageRange.getMaxAge());
            Date startDate = calendar.getTime();

            Long count = memberDao.countByBirthDayRange(startDate, endDate);
            resultList.add(count.intValue());
        }
        return resultList;
    }

    @Override
    public List<Integer> countBySexList(List<String> sexList) {
        List<Integer> resultList = new ArrayList<>();
        for (String sex : sexList) {
            Long count = memberDao.countBySex(sex);
            resultList.add(count.intValue());
        }
        return resultList;
    }

    @Override
    public List<Long> findMemberCountByMonth(List<String> monthList) {

        List<Long> longList = new ArrayList<>();

        //遍历月份,因sql中需要比较,所以将分别构造每个月的第一天和最后一天
        for (String month : monthList) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            //每个月第一天
            Date startDate = null;
            //每个月最后一天
            Date endDate = null;
            try {
                startDate = format.parse(month + "-1");
                endDate = format.parse(month + "-31");
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //通过每个月都第一天和最后一天查找这个月范围内的会员数量
            long countByMonth = memberDao.countByRegTimeRange(startDate, endDate);

            //将会员数量添加到list中
            longList.add(countByMonth);
        }
        //返回
        return longList;
    }
}
